﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using VMWorkshop.Domain;
using AutoMapper;

namespace VMWorkshop.Wpf.Models.ViewModels
{
    public class DemoViewModel : INotifyPropertyChanged
    {
        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Private fields
        private IList<ProductListView> _products;
        private ProductEditView _selectedProduct;
        private IProductRepository _repository;
        private Visibility _editFormVisibility = Visibility.Collapsed;
        private Visibility _updateMessageVisibility = Visibility.Collapsed;

        // Public properties
        public Visibility EditFormVisibility
        {
            get { return this._editFormVisibility; }
            set
            {
                if (this._editFormVisibility != value)
                {
                    this._editFormVisibility = value;
                    this.RaisePropertyChangedEvent("EditFormVisibility");
                }
            }
        }

        public Visibility UpdateMessageVisibility
        {
            get { return this._updateMessageVisibility; }
            set
            {
                if (this._updateMessageVisibility != value)
                {
                    this._updateMessageVisibility = value;
                    this.RaisePropertyChangedEvent("UpdateMessageVisibility");
                }
            }
        }

        public IList<ProductListView> Products
        {
            get 
            {
                if (this._products == null)
                {
                    this._products = this.GetProducts();
                }
                return this._products; 
            }
            set
            {
                if (this._products != value)
                {
                    this._products = value;
                    this.RaisePropertyChangedEvent("Products");
                }
            }
        }

        public ProductEditView SelectedProduct
        {
            get { return this._selectedProduct; }
            set
            {
                if (this._selectedProduct != value)
                {
                    this._selectedProduct = value;
                    this.RaisePropertyChangedEvent("SelectedProduct");
                }
            }
        }

        // Constructor
        public DemoViewModel(IProductRepository repository)
        {
            this._repository = repository;
        }

        // Public API methods
        public void Persist()
        {
            Product newProduct = ProductEditView.ToProduct(this.SelectedProduct);
            Product oldProduct = this._repository.GetProductById(newProduct.ProductId);
            oldProduct = Mapper.Map<Product, Product>(newProduct, oldProduct);
            this._repository.Persist(oldProduct);

            this.UpdateMessageVisibility = Visibility.Visible;
            this.EditFormVisibility = Visibility.Collapsed;
        }

        public void GetSelectedProduct(int productId)
        {
            Product product = this._repository.GetProductById(productId);
            this.SelectedProduct = ProductEditView.ToProductEditView(product);

            this.EditFormVisibility = Visibility.Visible;
            this.UpdateMessageVisibility = Visibility.Collapsed;
        }

        // Utilities
        private IList<ProductListView> GetProducts()
        {
            var products = this._repository.GetAll();
            return Mapper.Map<IList<Product>, IList<ProductListView>>(products);
        }

        protected virtual void RaisePropertyChangedEvent(params string[] propertyNames)
        {
            if (this.PropertyChanged != null)
            {
                foreach (string name in propertyNames)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
}
